//
//  UrbanAdvisoryViewController.m
//  UrbanManagement
//
//  Created by Can on 18/4/8.
//  Copyright © 2018年 hwd. All rights reserved.
//

#import "UrbanAdvisoryViewController.h"
#import "UrbanAdvisoryTableViewCell.h"
#import "UrbanNewsViewController.h"
#import "UrbanDetailsViewController.h"

@interface UrbanAdvisoryViewController ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UILabel *advisoryLable;//咨询lable
@property (nonatomic, strong) UIView *advisoryLine;//下滑线
@property (nonatomic, strong) UILabel *policyLable;//法规lable
@property (nonatomic, strong) UIView *policyLine;//下滑线
@property (nonatomic, strong) UIButton *newsBtn;//右侧按钮
@property (nonatomic, assign) NSInteger pageNumber;
@property (nonatomic, assign) NSInteger pageNewsNumber;
@property (nonatomic, strong) NSMutableArray *newsArray;
@property (nonatomic, strong) NSMutableArray *policyArray;
@property (nonatomic, assign) NSInteger typeNumber;
@property (nonatomic, assign) NSInteger advisoryCount;
@property (nonatomic, assign) NSInteger policyCount;

@end

@implementation UrbanAdvisoryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _pageNumber = 1;
    
    _pageNewsNumber = 1;
    
    _typeNumber = 1;
    
    _newsArray = [NSMutableArray new];
    
    _policyArray = [NSMutableArray new];
    
    [self creatView];
    
    [self creatHeadView];
    
    [self getRecordList];
}

- (void)creatView{
    CGFloat heigh = NavBarHeight;
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH - heigh) style:UITableViewStylePlain];
    _tableView.backgroundColor = Color(247, 247, 247);
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.showsHorizontalScrollIndicator = NO;
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
    _tableView.mj_header = [MJRefreshGifHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
}

- (void)creatHeadView
{
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, 44)];
    headView.backgroundColor = [UIColor clearColor];
    
    NSString *string = @"新闻咨询";
    CGFloat width = [TomamaUI getStringWeithtWith:string andHeigth:16 andFont:[UIFont systemFontOfSize:16]];

    //新闻咨询
    _advisoryLable = [TomamaUI createLabel:CGRectMake(SCREENW/2 - 35/2*screenW_6 - width, 0, width, 44) textAligment:1 font:[UIFont systemFontOfSize:16] textColor:[UIColor whiteColor] text:string];
    [headView addSubview:_advisoryLable];
    
    _advisoryLine = [[UIView alloc]initWithFrame:CGRectMake(0, 44 - 3*screenW_6, width + 20*screenW_6, 3*screenW_6)];
    _advisoryLine.backgroundColor = [UIColor whiteColor];
    _advisoryLine.hidden = NO;
    [headView addSubview:_advisoryLine];
    _advisoryLine.center = CGPointMake(_advisoryLable.center.x, _advisoryLine.center.y);

    UIButton *advisoryBtn = [TomamaUI createButton:0 Frame:CGRectMake(0, 0, _advisoryLine.frame.size.width, 44) targ:self sel:@selector(advisoryBtn) titleColor:nil font:nil title:nil image:nil backGroundImage:nil];
    advisoryBtn.backgroundColor = [UIColor clearColor];
    [headView addSubview:advisoryBtn];
    advisoryBtn.center = CGPointMake(_advisoryLable.center.x, advisoryBtn.center.y);
    
    //政策法规
    _policyLable = [TomamaUI createLabel:CGRectMake(CGRectGetMaxX(_advisoryLable.frame) + 35*screenW_6, 0, width, 44) textAligment:1 font:[UIFont systemFontOfSize:16] textColor:[UIColor whiteColor] text:@"政策法规"];
    [headView addSubview:_policyLable];
    
    _policyLine = [[UIView alloc]initWithFrame:CGRectMake(0, 44 - 3*screenW_6, width + 20*screenW_6, 3*screenW_6)];
    _policyLine.backgroundColor = [UIColor whiteColor];
    _policyLine.hidden = YES;
    [headView addSubview:_policyLine];
    _policyLine.center = CGPointMake(_policyLable.center.x, _policyLine.center.y);
    
    UIButton *policyBtn = [TomamaUI createButton:0 Frame:CGRectMake(0, 0, _policyLine.frame.size.width, 44) targ:self sel:@selector(policyBtn) titleColor:nil font:nil title:nil image:nil backGroundImage:nil];
    policyBtn.backgroundColor = [UIColor clearColor];
    [headView addSubview:policyBtn];
    policyBtn.center = CGPointMake(_policyLable.center.x, policyBtn.center.y);
    
    //按钮
    _newsBtn = [TomamaUI createButton:0 Frame:CGRectMake(SCREENW - 60, 0, 44, 44) targ:self sel:@selector(goNews) titleColor:nil font:nil title:nil image:@"tip_white" backGroundImage:nil];
    _newsBtn.backgroundColor = [UIColor clearColor];
    [headView addSubview:_newsBtn];
    
    self.navigationItem.titleView = headView;
}

- (void)refresh
{
    if (_typeNumber == 1) {
        _pageNumber = 1;
        [_newsArray removeAllObjects];
    }else{
        _pageNewsNumber = 1;
        [_policyArray removeAllObjects];
    }
    
    [self getRecordList];
}

//上拉加载更多
-(void)footerRefresh
{
    if (_typeNumber == 1) {
        _pageNumber++;
    }else{
        _pageNewsNumber++;
    }
    [self getRecordList];
}

#pragma mark - tabledelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if (_typeNumber == 1) {
        return _newsArray.count;
    }else{
        return _policyArray.count;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

//section头部间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 1;//section头部高度
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [UIView new];
    return view;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellID = @"AdvisoryCell";
    UrbanAdvisoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UrbanAdvisoryTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    
    if (_typeNumber == 1) {
        [cell loadData:_newsArray[indexPath.section]];
    }else{
        [cell loadData:_policyArray[indexPath.section]];
    }
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_typeNumber == 1) {
        return [UrbanAdvisoryTableViewCell cellHeight:_newsArray[indexPath.section]];
    }else{
        return [UrbanAdvisoryTableViewCell cellHeight:_policyArray[indexPath.section]];
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UrbanDetailsViewController *detailVc = [UrbanDetailsViewController new];
    if (_typeNumber == 1) {
        detailVc.ID = [_newsArray[indexPath.section] objectForKey:@"id"];
    }else{
        detailVc.ID = [_policyArray[indexPath.section] objectForKey:@"id"];
    }
    [self.navigationController pushViewController:detailVc animated:YES];
}

//咨询
- (void)advisoryBtn
{
    _typeNumber = 1;
    _advisoryLine.hidden = NO;
    _policyLine.hidden = YES;
    _advisoryLable.textColor = [UIColor whiteColor];
    _policyLable.textColor = [UIColor whiteColor];
    

    if (_pageNumber == 1) {
        [_newsArray removeAllObjects];
        [self getRecordList];
    }else{
        [_tableView reloadData];
    }
    
    if (_advisoryCount/10 <= _pageNumber) {
        self.tableView.mj_footer.hidden = YES;
    }else{
        self.tableView.mj_footer.hidden = NO;
    }
}

//政策法规
- (void)policyBtn
{
    _typeNumber = 2;
    _advisoryLine.hidden = YES;
    _policyLine.hidden = NO;
    _advisoryLable.textColor = [UIColor whiteColor];
    _policyLable.textColor = [UIColor whiteColor];
    if (_pageNewsNumber == 1) {
        [_policyArray removeAllObjects];
        [self getRecordList];
    }else{
        [_tableView reloadData];
    }
    
    if (_policyCount/10 <= _pageNewsNumber) {
        self.tableView.mj_footer.hidden = YES;
    }else{
        self.tableView.mj_footer.hidden = NO;
    }
}

- (void)goNews
{
    [self.navigationController pushViewController:[UrbanNewsViewController new] animated:YES];
}

- (void)getRecordList
{
    [HudHelper showHudWithMessage:@"" toView:self.view];
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        
        NSMutableDictionary *dic = [NSMutableDictionary new];
        [dic setValue:[NSString stringWithFormat:@"%ld",(long)self->_typeNumber] forKey:@"newsType"];
        if (self->_typeNumber == 1) {
            [dic setValue:[NSString stringWithFormat:@"%ld",(long)self->_pageNumber] forKey:@"pageNo"];
        }else{
            [dic setValue:[NSString stringWithFormat:@"%ld",(long)self->_pageNewsNumber] forKey:@"pageNo"];
        }
        [dic setValue:@"10" forKey:@"pageSize"];
        
        [HttpRequest PostrequestUrl:@"jeesite/appInterface/news/newsList" Dic:dic completion:^(NSDictionary *responseDic){
//            NSLog(@"responseDic:%@",responseDic);
            dispatch_async(dispatch_get_main_queue(), ^{
                [HudHelper hideHudToView:self.view];
                if ([responseDic[@"success"] boolValue]) {//请求成功
                    if (self->_typeNumber == 1) {
                        self->_advisoryCount = [[[responseDic objectForKey:@"result"] objectForKey:@"count"] integerValue];
                        [self->_newsArray addObjectsFromArray:[[responseDic objectForKey:@"result"] objectForKey:@"list"]];
                    }else{
                        self->_policyCount = [[[responseDic objectForKey:@"result"] objectForKey:@"count"] integerValue];
                        [self->_policyArray addObjectsFromArray:[[responseDic objectForKey:@"result"] objectForKey:@"list"]];
                    }
                    
                    [self->_tableView reloadData];
                }
                
                if ([self->_tableView.mj_header isRefreshing]) {
                    [self->_tableView.mj_header endRefreshing];
                }
                if ([self->_tableView.mj_footer isRefreshing]) {
                    
                    [self->_tableView.mj_footer endRefreshing];
                }
                if (self->_typeNumber == 1) {
                    if (self->_pageNumber >= self->_advisoryCount/10) {
                        self.tableView.mj_footer.hidden = YES;
                    }else{
                        self.tableView.mj_footer.hidden = NO;
                    }
                }else{
                    if (self->_pageNewsNumber >= self->_policyCount/10) {
                        self.tableView.mj_footer.hidden = YES;
                    }else{
                        self.tableView.mj_footer.hidden = NO;
                    }
                }
            });
        }];
    });
}

-(UIImage *)colorizeImage:(UIImage *)baseImage withColor:(UIColor *)theColor {
    UIGraphicsBeginImageContext(baseImage.size);
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGRect area = CGRectMake(0, 0, baseImage.size.width, baseImage.size.height);
    
    CGContextScaleCTM(ctx, 1, -1);
    CGContextTranslateCTM(ctx, 0, -area.size.height);
    
    CGContextSaveGState(ctx);
    CGContextClipToMask(ctx, area, baseImage.CGImage);
    
    [theColor set];
    CGContextFillRect(ctx, area);
    
    CGContextRestoreGState(ctx);
    
    CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
    
    CGContextDrawImage(ctx, area, baseImage.CGImage);
    
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return newImage;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
